$(document).ready(function () {
    $('#checkpsw').off('click').click(function () {
        if ($('#login-password').attr('type') == "password") {
            $('#login-password').attr('type', 'text')
        } else {
            $('#login-password').attr('type', 'password')
        }
    })

    if (getCookie('remember') == 1100110101) {
        $("#remember").prop("checked", true)
        $('#login-username').val(getCookie('remember-username'))
        if(getCookie('remember-usertype')=='student'){
            $('#login-stu').prop('checked',true)
        }else if(getCookie('remember-usertype')=='teacher'){
            $('#login-tea').prop('checked',true)
        }else if(getCookie('remember-usertype')=='clerk'){
            $('#login-sjy').prop('checked',true)
        }
    }
    $('#login-sumbit').off('click').click(function () {
        // var pswmd5=$('#login-password').val()
        // if(pswmd5.trim().length<32){
        //     pswmd5=hex_md5(pswmd5);
        // }
        if($('#login-username').val().length>0&&$('#login-password').val().length>0){
            if ($('#remember').prop('checked') == true) {
                setCookie('remember', 1100110101)
                setCookie('remember-username', $('#login-username').val())
                setCookie('remember-usertype',$('input[name=logintype]:checked').attr('value'))
            } else {
                clearCookie('remember')
                clearCookie('remember-username')
                clearCookie('remember-usertype')
            }
            $.ajax({
                type: 'POST',
                url: '/common/login',
                data: {
                    "username": $('#login-username').val(),
                    "password": $('#login-password').val(),
                    "type": $('input[name=logintype]:checked').attr('value'),
                },
                xhrFields: {
                    withCredentials: true
                },
                 beforeSend:function(){
                    $('#login-username').attr({ disabled: "disabled" });
                    $('#login-username').html('正在处理请求...')
                 },
                success: function (data) {
                    console.log(data)
                    if (data.status == 0) {
                        if ($('#remember').prop('checked') == true) {
                            setCookie('remember', 1100110101)
                            setCookie('remember-username', $('#login-username').val())
                            setCookie('remember-usertype',$('input[name=logintype]:checked').attr('value'))
                        } else {
                            clearCookie('remember')
                            clearCookie('remember-username')
                            clearCookie('remember-usertype')
                        }
                        setCookie('u-college',data.college)
                        setCookie('u-grade',data.grade)
                        setCookie('u-sno',data.sno)
                        setCookie('u-major',data.major)
                        setCookie('u-clazz',data.clazz)
                        setCookie('u-name', data.name)
                        setCookie('login-usertype',$('input[name=logintype]:checked').attr('value'))
                        if($('input[name=logintype]:checked').attr('value')=="student"){
                            setCookie('stuid', data.sid)
                        }
                        if($('input[name=logintype]:checked').attr('value')=="teacher"){
                            setCookie('teaid', data.tid)
                        }
                        if($('input[name=logintype]:checked').attr('value')=="clerk"){
                            setCookie('cleid', data.cid)
                        }
                        window.location.href = "Personnel-list.html"
                    } else{
                        $('#myModal-public').modal({})
                        $('#myModal-public .modal-title').html('登录失败')
                        $('#myModal-public-body').html(data.message)
                        $('#login-check').html('')
                    }
                },
                complete: function () {
                    $("#login-username").removeAttr("disabled");
                    $('#login-username').html('登陆')
                }
                 
            })
            
           
        }else{
            $('#login-check').html('')
            $('#login-sumbit').after('<p style="color:red" id="login-check">账号和密码不能为空！</p>')
        }
       
    })

})